package lab03.Test06;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lady on 2017/12/13.
 */
public class Test {
    public  static void main(String[] args) {
        String path="F:\\大二上学期\\Java\\实验读写文件\\test6.txt";
        RandomAccessFile file=null;
        Map<String,Integer> map=new HashMap<>();

        try {
            try {
                file=new RandomAccessFile(path,"r");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            String line=null;
            while ((line=file.readLine())!=null){
                System.out.println(line);

                //先判断map中有没有这个key，没有直接将value赋值为1
                if(map.get(line)==null){
                    map.put(line,1);
                }else {

                    int value= map.get(line);
                    value=value+1;
                    map.put(line,value);
                    //或者一行代码：
                    map.put(line,map.get(line)+1);

                }
            }
            //map
            // key(String)---value(int)
            //run     3
            // world   1
            // study   1
            // man     0
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
